package com.inei.cie.capitulovi;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import com.inei.cie.Carga_Trabajo_Activity;
import com.inei.cie.R;
import com.inei.cie.capituloix.CapituloIX_Activity;
import com.inei.cie.dao.CapituloVIDao;
import com.inei.cie.entidades.P6_1;
import com.inei.cie.entidades.P6_1_10N;
import com.inei.cie.entidades.P6_1_8N;

public class CapituloVI_SeccionC_Instalaciones_Electricas_Interiores_Activity
		extends Activity {

	public int Nro_Ed, Nro_Pred;
	public String edificacion, id_local;
	public P6_1 obj;
	public List<P6_1_8N> lObj8;
	public List<P6_1_10N> lObj10;

	public CapituloVIDao cap6dao;

	// Componentes

	public TableLayout tl6C;
	public TableRow tr6C1, tr6C1A, tr6C2, tr6C2A, tr6C2B, tr6C2C, tr6C2D,
			tr6C3, tr6C3A;
	public Spinner sp6C1A;
	public RadioGroup rg6C1, rg6C2, rg6C3;
	public RadioButton rb6C1si, rb6C1no, rb6C2si, rb6C2no, rb6C3si, rb6C3no;
	public TextView tvEdificacionNro;

	// Opciones de la Pregunta 2

	public RadioButton rb6C2Asi, rb6C2Ano, rb6C2Bsi, rb6C2Bno, rb6C2Csi,
			rb6C2Cno, rb6C2Dsi, rb6C2Dno;
	public EditText et6C3Aanio;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.capitulo_vi_seccion_c_instalaciones_electricas_interiores);

		IniciarComponentes();

		rg6C1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch (checkedId) {
				case R.id.rb6C1si:
					if (rb6C1si.isChecked()) {
						tl6C.removeView(tr6C1A);
						tl6C.removeView(tr6C2);
						tl6C.removeView(tr6C2A);
						tl6C.removeView(tr6C2B);
						tl6C.removeView(tr6C2C);
						tl6C.removeView(tr6C2D);
						tl6C.removeView(tr6C3);
						tl6C.removeView(tr6C3A);
						tl6C.addView(tr6C1A, tl6C.indexOfChild(tr6C1) + 1);
						tl6C.addView(tr6C2, tl6C.indexOfChild(tr6C1) + 2);
					}
					break;

				case R.id.rb6C1no:
					if (rb6C1no.isChecked()) {
						rb6C2si.setChecked(false);
						rb6C2Asi.setChecked(false);
						rb6C2Bsi.setChecked(false);
						rb6C2Csi.setChecked(false);
						rb6C2Dsi.setChecked(false);
						rb6C3si.setChecked(false);
						rb6C2no.setChecked(false);
						rb6C2Ano.setChecked(false);
						rb6C2Bno.setChecked(false);
						rb6C2Cno.setChecked(false);
						rb6C2Dno.setChecked(false);
						rb6C3no.setChecked(false);
						sp6C1A.setSelection(0);
						et6C3Aanio.setText("");
						tl6C.removeView(tr6C1A);
						tl6C.removeView(tr6C2);
						tl6C.removeView(tr6C2A);
						tl6C.removeView(tr6C2B);
						tl6C.removeView(tr6C2C);
						tl6C.removeView(tr6C2D);
						tl6C.removeView(tr6C3);
						tl6C.removeView(tr6C3A);
					}
					break;

				default:
					break;
				}
			}
		});

		rg6C2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch (checkedId) {
				case R.id.rb6C2si:
					if (rb6C2si.isChecked()) {
						tl6C.removeView(tr6C2A);
						tl6C.removeView(tr6C2B);
						tl6C.removeView(tr6C2C);
						tl6C.removeView(tr6C2D);
						tl6C.removeView(tr6C3);
						tl6C.removeView(tr6C3A);
						tl6C.addView(tr6C2A, tl6C.indexOfChild(tr6C2) + 1);
						tl6C.addView(tr6C2B, tl6C.indexOfChild(tr6C2) + 2);
						tl6C.addView(tr6C2C, tl6C.indexOfChild(tr6C2) + 3);
						tl6C.addView(tr6C2D, tl6C.indexOfChild(tr6C2) + 4);
						tl6C.addView(tr6C3, tl6C.indexOfChild(tr6C2) + 5);
					}
					break;

				case R.id.rb6C2no:
					if (rb6C2no.isChecked()) {
						rb6C2Asi.setChecked(false);
						rb6C2Bsi.setChecked(false);
						rb6C2Csi.setChecked(false);
						rb6C2Dsi.setChecked(false);
						rb6C3si.setChecked(false);
						rb6C2Ano.setChecked(false);
						rb6C2Bno.setChecked(false);
						rb6C2Cno.setChecked(false);
						rb6C2Dno.setChecked(false);
						rb6C3no.setChecked(false);
						et6C3Aanio.setText("");
						tl6C.removeView(tr6C2A);
						tl6C.removeView(tr6C2B);
						tl6C.removeView(tr6C2C);
						tl6C.removeView(tr6C2D);
						tl6C.removeView(tr6C3);
						tl6C.removeView(tr6C3A);
					}
					break;

				default:
					break;
				}
			}
		});

		rg6C3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch (checkedId) {
				case R.id.rb6C3si:
					if (rb6C3si.isChecked()) {
						tl6C.removeView(tr6C3A);
						tl6C.addView(tr6C3A, tl6C.indexOfChild(tr6C3) + 1);
						et6C3Aanio.setText("");
					}
					break;

				case R.id.rb6C3no:
					if (rb6C3no.isChecked()) {
						tl6C.removeView(tr6C3A);
					}
					break;

				default:
					break;
				}
			}
		});
		CargarDatos();
	}

	public void IniciarComponentes() {

		tvEdificacionNro = (TextView) findViewById(R.id.tvEdificacionNro);

		tl6C = (TableLayout) findViewById(R.id.tl6C);
		tr6C1 = (TableRow) findViewById(R.id.tr6C1);
		tr6C1A = (TableRow) findViewById(R.id.tr6C1A);
		tr6C2 = (TableRow) findViewById(R.id.tr6C2);
		tr6C2A = (TableRow) findViewById(R.id.tr6C2A);
		tr6C2B = (TableRow) findViewById(R.id.tr6C2B);
		tr6C2C = (TableRow) findViewById(R.id.tr6C2C);
		tr6C2D = (TableRow) findViewById(R.id.tr6C2D);
		tr6C3 = (TableRow) findViewById(R.id.tr6C3);
		tr6C3A = (TableRow) findViewById(R.id.tr6C3A);

		rg6C1 = (RadioGroup) findViewById(R.id.rg6C1);
		rb6C1si = (RadioButton) findViewById(R.id.rb6C1si);
		rb6C1no = (RadioButton) findViewById(R.id.rb6C1no);
		rg6C2 = (RadioGroup) findViewById(R.id.rg6C2);
		rb6C2si = (RadioButton) findViewById(R.id.rb6C2si);
		rb6C2no = (RadioButton) findViewById(R.id.rb6C2no);
		rg6C3 = (RadioGroup) findViewById(R.id.rg6C3);
		rb6C3si = (RadioButton) findViewById(R.id.rb6C3si);
		rb6C3no = (RadioButton) findViewById(R.id.rb6C3no);

		sp6C1A = (Spinner) findViewById(R.id.sp6C1A);

		rb6C2Asi = (RadioButton) findViewById(R.id.rb6C2Asi);
		rb6C2Ano = (RadioButton) findViewById(R.id.rb6C2Ano);
		rb6C2Bsi = (RadioButton) findViewById(R.id.rb6C2Bsi);
		rb6C2Bno = (RadioButton) findViewById(R.id.rb6C2Bno);
		rb6C2Csi = (RadioButton) findViewById(R.id.rb6C2Csi);
		rb6C2Cno = (RadioButton) findViewById(R.id.rb6C2Cno);
		rb6C2Dsi = (RadioButton) findViewById(R.id.rb6C2Dsi);
		rb6C2Dno = (RadioButton) findViewById(R.id.rb6C2Dno);

		et6C3Aanio = (EditText) findViewById(R.id.et6C3Aanio);

		tl6C.removeView(tr6C1A);
		tl6C.removeView(tr6C2);
		tl6C.removeView(tr6C2A);
		tl6C.removeView(tr6C2B);
		tl6C.removeView(tr6C2C);
		tl6C.removeView(tr6C2D);
		tl6C.removeView(tr6C3);
		tl6C.removeView(tr6C3A);
	}

	public void CargarDatos() {
		id_local = getIntent().getExtras().getString("id_local");
		Nro_Ed = getIntent().getExtras().getInt("Nro_Ed");
		Nro_Pred = getIntent().getExtras().getInt("Nro_Pred");
		edificacion = "Local : "
				+ id_local
				+ " - Predio : "
				+ (getIntent().getExtras().getInt("TITULO_PRED") < 10 ? "0"
						: "") + getIntent().getExtras().getInt("TITULO_PRED")
				+ " - Edificaci\u00f3n Nro. " + (Nro_Ed < 10 ? "0" : "")
				+ Nro_Ed;
		tvEdificacionNro.setText(edificacion);

		cap6dao = new CapituloVIDao(getApplicationContext());
		List<Object> listaDatos = cap6dao.obtenerEdificacionA(id_local, Nro_Ed,
				Nro_Pred);
		obj = (P6_1) listaDatos.get(0);
		lObj8 = (ArrayList<P6_1_8N>) listaDatos.get(1);
		lObj10 = (ArrayList<P6_1_10N>) listaDatos.get(2);

		if (obj.getP6_3_1() != 0) {
			if (obj.getP6_3_1() == 1) {
				rb6C1si.setChecked(true);
			} else {
				rb6C1no.setChecked(true);
			}
		}

		if (obj.getP6_3_1A() != 0) {
			sp6C1A.setSelection(obj.getP6_3_1A());
		}

		if (obj.getP6_3_2() != 0) {
			if (obj.getP6_3_2() == 1) {
				rb6C2si.setChecked(true);
			} else {
				rb6C2no.setChecked(true);
			}
		}

		if (obj.getP6_3_2A() != 0) {
			if (obj.getP6_3_2A() == 1) {
				rb6C2Asi.setChecked(true);
			} else {
				rb6C2Ano.setChecked(true);
			}
		}

		if (obj.getP6_3_2B() != 0) {
			if (obj.getP6_3_2B() == 1) {
				rb6C2Bsi.setChecked(true);
			} else {
				rb6C2Bno.setChecked(true);
			}
		}

		if (obj.getP6_3_2C() != 0) {
			if (obj.getP6_3_2C() == 1) {
				rb6C2Csi.setChecked(true);
			} else {
				rb6C2Cno.setChecked(true);
			}
		}

		if (obj.getP6_3_2D() != 0) {
			if (obj.getP6_3_2D() == 1) {
				rb6C2Dsi.setChecked(true);
			} else {
				rb6C2Dno.setChecked(true);
			}
		}

		if (obj.getP6_3_3() != 0) {
			if (obj.getP6_3_3() == 1) {
				rb6C3si.setChecked(true);
				if (obj.getP6_3_3A() != 0) {
					et6C3Aanio.setText(obj.getP6_3_3A() + "");
				}
			} else if (obj.getP6_3_3() == 2) {
				rb6C3no.setChecked(true);
			}
		}

		if (obj.getP6_3_3A() != 0) {
			et6C3Aanio.setText(obj.getP6_3_3A() + "");
		}
	}

	public void guardar() {
		obj.setP6_3_1(rb6C1si.isChecked() ? 1 : (rb6C1no.isChecked() ? 2 : 0));
		obj.setP6_3_1A(sp6C1A.getSelectedItemPosition());
		obj.setP6_3_2(rb6C2si.isChecked() ? 1 : (rb6C2no.isChecked() ? 2 : 0));
		obj.setP6_3_2A(rb6C2Asi.isChecked() ? 1
				: (rb6C2Ano.isChecked() ? 2 : 0));
		obj.setP6_3_2B(rb6C2Bsi.isChecked() ? 1
				: (rb6C2Bno.isChecked() ? 2 : 0));
		obj.setP6_3_2C(rb6C2Csi.isChecked() ? 1
				: (rb6C2Cno.isChecked() ? 2 : 0));
		obj.setP6_3_2D(rb6C2Dsi.isChecked() ? 1
				: (rb6C2Dno.isChecked() ? 2 : 0));
		obj.setP6_3_3(rb6C3si.isChecked() ? 1 : (rb6C3no.isChecked() ? 2 : 0));
		if (et6C3Aanio.getText().toString().equals("")) {
			obj.setP6_3_3A(0);
		} else {
			obj.setP6_3_3A(Integer.parseInt(et6C3Aanio.getText().toString()));
		}
		cap6dao.actualizarEdificacionA(obj, lObj8, lObj10);

		Intent irMenu = new Intent(
				this,
				CapituloVI_SeccionD_Instalaciones_Sanitarias_Interiores_Activity.class);
		irMenu.putExtras(getIntent().getExtras());
		startActivity(irMenu);
		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.menu_capitulovi, menu);
		menu.getItem(0).setTitle("SECCIONES");
		menu.getItem(1).setTitle("SIGUIENTE SECCI\u00d3N");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub

		switch (item.getItemId()) {
		case R.id.mnSalir:
			AlertDialog.Builder dialogo1 = new AlertDialog.Builder(this);
			dialogo1.setTitle("Importante");
			dialogo1.setMessage("Deseas Salir ?");
			dialogo1.setIcon(android.R.drawable.ic_dialog_alert);
			dialogo1.setCancelable(false);
			dialogo1.setPositiveButton("Si",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							System.exit(0);
						}
					});

			dialogo1.setNegativeButton("No",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							return;
						}
					});
			dialogo1.show();
			break;
		case R.id.mnMarco:
			Intent irMarco = new Intent(this, Carga_Trabajo_Activity.class);
			irMarco.putExtras(getIntent().getExtras());
			startActivity(irMarco);
			finish();
			break;
		case R.id.mnIncio:
			Intent marco = new Intent(this, CapituloVI_Menu_Activity.class);
			marco.putExtras(getIntent().getExtras());
			startActivity(marco);
			finish();
			break;
		case R.id.mnSiguiente:
			int p6_1 = rb6C1si.isChecked() ? 1 : (rb6C1no.isChecked() ? 2 : 0);
			int p6_1a = sp6C1A.getSelectedItemPosition();
			int p6_2 = rb6C2si.isChecked() ? 1 : (rb6C2no.isChecked() ? 2 : 0);
			int p6_2a = rb6C2Asi.isChecked() ? 1 : (rb6C2Ano.isChecked() ? 2
					: 0);
			int p6_2b = rb6C2Bsi.isChecked() ? 1 : (rb6C2Bno.isChecked() ? 2
					: 0);
			int p6_2c = rb6C2Csi.isChecked() ? 1 : (rb6C2Cno.isChecked() ? 2
					: 0);
			int p6_2d = rb6C2Dsi.isChecked() ? 1 : (rb6C2Dno.isChecked() ? 2
					: 0);
			int p6_3 = rb6C3si.isChecked() ? 1 : (rb6C3no.isChecked() ? 2 : 0);
			int p6_3a = Integer.parseInt(et6C3Aanio.getText().toString()
					.length() == 0 ? "0" : et6C3Aanio.getText().toString());

			if (p6_1 == 0) {
				Toast.makeText(
						CapituloVI_SeccionC_Instalaciones_Electricas_Interiores_Activity.this,
						"Debe seleccionar una opci\u00f3n en la pregunta 1.",
						Toast.LENGTH_SHORT).show();
				break;
			}
			if (p6_1 == 1) {
				if (p6_1a == 0) {
					Toast.makeText(
							CapituloVI_SeccionC_Instalaciones_Electricas_Interiores_Activity.this,
							"Debe seleccioanr una opci\u00f3n en la pregunta 1.A.",
							Toast.LENGTH_SHORT).show();
					break;
				}
				if (p6_2 == 0) {
					Toast.makeText(
							CapituloVI_SeccionC_Instalaciones_Electricas_Interiores_Activity.this,
							"Debe seleccionar una opci\u00f3n en la pregunta 2.",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
			if (p6_2 == 1) {
				if (p6_2a == 0) {
					Toast.makeText(
							CapituloVI_SeccionC_Instalaciones_Electricas_Interiores_Activity.this,
							"Debe seleccionar una opci\u00f3n en la pregunta 2.A.",
							Toast.LENGTH_SHORT).show();
					break;
				}
				if (p6_2b == 0) {
					Toast.makeText(
							CapituloVI_SeccionC_Instalaciones_Electricas_Interiores_Activity.this,
							"Debe seleccionar una opci\u00f3n en la pregunta 2.B.",
							Toast.LENGTH_SHORT).show();
					break;
				}
				if (p6_2c == 0) {
					Toast.makeText(
							CapituloVI_SeccionC_Instalaciones_Electricas_Interiores_Activity.this,
							"Debe seleccionar una opci\u00f3n en la pregunta 2.C.",
							Toast.LENGTH_SHORT).show();
					break;
				}
				if (p6_2d == 0) {
					Toast.makeText(
							CapituloVI_SeccionC_Instalaciones_Electricas_Interiores_Activity.this,
							"Debe seleccionar una opci\u00f3n en la pregunta 2.D.",
							Toast.LENGTH_SHORT).show();
					break;
				}
				if (p6_3 == 0) {
					Toast.makeText(
							CapituloVI_SeccionC_Instalaciones_Electricas_Interiores_Activity.this,
							"Debe seleccionar una opci\u00f3n en la pregunta 3.",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}

			if (p6_3 == 1) {
				if (p6_3a == 0) {
					Toast.makeText(
							CapituloVI_SeccionC_Instalaciones_Electricas_Interiores_Activity.this,
							"Debe indicar el a\u00f1o en la pregunta 3.A.",
							Toast.LENGTH_SHORT).show();
					break;
				}
				if (p6_3a < 1900 || p6_3a > 2013) {
					Toast.makeText(
							CapituloVI_SeccionC_Instalaciones_Electricas_Interiores_Activity.this,
							"El a\u00f1o debe estar entre 1900 y 2013 en la pregunta 3.A.",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}

			guardar();
			break;
		case R.id.mnFoto:
			Intent foto = new Intent(
					CapituloVI_SeccionC_Instalaciones_Electricas_Interiores_Activity.this,
					CapituloIX_Activity.class);
			// b.putBoolean("pregunta", true);
			foto.putExtras(getIntent().getExtras());
			foto.putExtra("pregunta", true);
			startActivity(foto);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		Intent irPredio = new Intent(this, CapituloVI_Menu_Activity.class);
		irPredio.putExtras(getIntent().getExtras());
		startActivity(irPredio);
		finish();
	}

}
